package uz.edo.app.client;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.client.core.MySession;
import uz.edo.db.bean.User;
import uz.edo.db.manager.UserManager;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/17/12
 * Time: 10:54 AM
 * To change this template use File | Settings | File Templates.
 */
public class Login extends WebPage {
    private User login;
    private Form form;

    public Login() {
        login = new User();
        CompoundPropertyModel userModel = new CompoundPropertyModel(login);
        form = new Form("loginForm",userModel) {
            @Override
            protected void onSubmit() {
                try {
                    MySession session = (MySession)getSession();
                    User user = UserManager.getInstance().getUser(login.getUserName(), login.getPassword());
                    if (user != null && "true".equals(user.getActive())) {
                        session.setUser(user);
                        setResponsePage(Home.class);
                    } else {
                        error("Login yoki Parolingiz hato");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        form.add(new TextField("userName").setRequired(true));
        form.add(new PasswordTextField("password").setRequired(true));
        form.add(new FeedbackPanel("message"));
        add(form);
    }
}
